﻿using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BillSADTool
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private string _ldapPath = "LDAP://vail.rubios.com/";
        private string _username = "Domain1\\" + "t-tranney"; // replace with your username here
        private string _password = "Iamawesom3"; // replace with your password here

        public Dictionary<String, String> ADProperties { get; set; }
        public List<String> ADResults { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonOpen_Click(object sender, EventArgs e)
        {
            using (System.DirectoryServices.DirectoryEntry RootDSE = new DirectoryEntry(_ldapPath + "RootDSE", _username, _password))
            {
                dataGridView1.Rows.Clear();
                String DomainDN = RootDSE.Properties["DefaultNamingContext"].Value.ToString();
                using (DirectoryEntry ADEntry = new DirectoryEntry(_ldapPath + DomainDN, _username, _password))
                {
                    DirectorySearcher ADSearch = new System.DirectoryServices.DirectorySearcher(ADEntry);
                    ADSearch.Filter = ("(&(objectCategory=person)(objectClass=user)(|(samAccountName=" + textBoxSam.Text + "*)(displayName=" + textBoxSam.Text + "*)(sn=" + textBoxSam.Text + "*)))");
                    ADSearch.SearchScope = SearchScope.Subtree;
                    SearchResult UserFound = ADSearch.FindOne();
                    if (UserFound != null)
                    {
                        comboBoxProperties.Items.Clear();
                        // In case we need to find where a user or other item is coming from look at the path.
                        //MessageBox.Show(UserFound.GetDirectoryEntry().Path.ToString());   
                        ADProperties = new Dictionary<string, string>();
                        foreach (PropertyValueCollection prop in UserFound.GetDirectoryEntry().Properties)
                        {
                            ADProperties.Add(prop.PropertyName.ToString(), prop.Value.ToString());
                            comboBoxProperties.Items.Add(prop.PropertyName.ToString());
                            DataGridViewCell ce = new DataGridViewTextBoxCell();
                            ce.Value = prop.PropertyName.ToString();
                            using (DataGridViewRow row = new DataGridViewRow())
                            {
                         		row.Cells.Add(ce);
                           		ce = new DataGridViewTextBoxCell();
                           		ce.Value = prop.Value.ToString();
                           		row.Cells.Add(ce);
                                dataGridView1.Rows.Add(row);
                            }
                            
                        }
                    }
                }
            }
        }

        private void buttonQuery_Click(object sender, EventArgs e)
        {
            Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
            Application.DoEvents();
            using (System.DirectoryServices.DirectoryEntry RootDSE = new DirectoryEntry(_ldapPath + "RootDSE", _username, _password))
            {
                String DomainDN = RootDSE.Properties["DefaultNamingContext"].Value.ToString();
                using (DirectoryEntry ADEntry = new DirectoryEntry(_ldapPath + comboBoxSubTree.SelectedItem.ToString() + "," + DomainDN, _username, _password))
                {
                    ADResults = new List<string>();
                    DirectorySearcher ADSearch = new System.DirectoryServices.DirectorySearcher(ADEntry);
                    ADSearch.SearchScope = SearchScope.Subtree;
                    SearchResultCollection cnsFound = ADSearch.FindAll();
                    foreach (SearchResult item in cnsFound)
                    {
                        if (item.GetDirectoryEntry().Properties.Contains(comboBoxProperties.SelectedItem.ToString()))
                        {
                            //ADResults.Add(item.GetDirectoryEntry().Properties["displayName"].Value.ToString() + " \t: " + item.GetDirectoryEntry().Properties[comboBoxProperties.SelectedItem.ToString()].Value.ToString());
                            if (!ADResults.Contains(item.GetDirectoryEntry().Properties[comboBoxProperties.SelectedItem.ToString()].Value.ToString()))
                            {
                                ADResults.Add(item.GetDirectoryEntry().Properties[comboBoxProperties.SelectedItem.ToString()].Value.ToString());
                            }
                        }
                        else
                        {
                            //ADResults.Add(item.GetDirectoryEntry().Properties["cn"].Value.ToString() + " \t: -");
                        }
                    }
                    listBoxResults.Items.Clear();
                    listBoxResults.Items.AddRange(ADResults.ToArray());
                }
            }
            Cursor.Current = System.Windows.Forms.Cursors.Default;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            System.Security.Principal.WindowsIdentity UserPrincipal = System.Security.Principal.WindowsIdentity.GetCurrent();
            int indexof = UserPrincipal.Name.IndexOf("\\");
            textBoxSam.Text = UserPrincipal.Name.Substring(indexof + 1, UserPrincipal.Name.Length - indexof - 1);
            comboBoxSubTree.SelectedIndex = 0;
        }




    }
}
